#include <QtGui>
#include <QDebug>

#include "typeSearch.h"
#include "resultItem.h"
#include "database_connection.h"

TypeSearch::TypeSearch(QWidget *parent)
    : QDialog(parent)
{
    resize(800, 480);

    isEmpty = 0;  //初始化未找到结果
    pageSeq = 1;

    areaLabel = new QLabel("区号:", this);
    devLabel = new QLabel("设备号:", this);
    tmpLabel = new QLabel("未找到结果", this);
    area = new QComboBox(this);
    dev = new QComboBox(this);
    pageUp = new QPushButton("上一页", this);
    pageDown = new QPushButton("下一页", this);
    groupBox = new QGroupBox(this);

    QFont tmpFont = tmpLabel->font();
    tmpFont.setPointSize(50);
    tmpLabel->setFont(tmpFont);
    tmpLabel->setAlignment(Qt::AlignCenter);

    area->addItem("A");
    area->addItem("B");
    dev->addItem("排烟阀");
    dev->addItem("消防水泵阀");
    dev->addItem("声光警铃");
    dev->addItem("消防广播");

    connect(area, SIGNAL(currentIndexChanged(int)), this, SLOT(updateResult()));
    connect(dev, SIGNAL(currentIndexChanged(int)), this, SLOT(updateResult()));
    connect(pageUp, SIGNAL(clicked()), this, SLOT(pageSeqDes()));
    connect(pageDown, SIGNAL(clicked()), this, SLOT(pageSeqInc()));

    QHBoxLayout *topLayout = new QHBoxLayout;
    topLayout->addWidget(areaLabel);
    topLayout->addWidget(area);
    topLayout->addWidget(devLabel);
    topLayout->addWidget(dev);
    topLayout->addStretch();
    topLayout->addWidget(pageUp);
    topLayout->addWidget(pageDown);

    bottomLayout= new QGridLayout;

    mainLayout = new QVBoxLayout;
    mainLayout->addLayout(topLayout);
    mainLayout->addWidget(groupBox);
    mainLayout->addWidget(tmpLabel);

    setLayout(mainLayout);
    setWindowTitle(tr("类型查询"));

    updateResult();
}

void TypeSearch::pageSeqInc()
{
    if(pageSeq == maxPages)
    {
        qDebug() << "最后一页";
    }
    else
    {
        pageSeq++;
        updateResult();
    }
}

void TypeSearch::pageSeqDes()
{
    if(pageSeq == 1)
    {
        qDebug() << "已到首页";
    }
    else
    {
        pageSeq--;
        updateResult();
    }
}

void TypeSearch::updateResult()
{
    delete bottomLayout;
    numOfItem = 0;
    QSqlDatabase::database().transaction();
    QSqlQuery query;

    QString areaTemp = area->currentText() + QString("区");
    QString devNameTemp = dev->currentText();

    query.exec(QString("select id from locate_information_table where district = '%1' and device_name = '%2'").arg(areaTemp).arg(devNameTemp));
    while(query.next())
    {
        numOfItem++;
    }
    maxPages = numOfItem / numOfPerPage + 1;
    qDebug() << QString("max is %1").arg(maxPages);
    query.exec(QString("select id, district, building, layer, room, device_name from"
                       "(select * from locate_information_table where district = '%1' and device_name = '%2') limit %3, %4").arg(areaTemp).arg(devNameTemp).arg((pageSeq-1)*20).arg(numOfPerPage));

    int i = 0;//循环获取数据库查询数据的计数
    while(query.next())
    {
        item[i] = new ResultItem;
        item[i]->setNum(query.value(0).toString());

        QString areaTemp = query.value(1).toString();
        QString ridgepoleTemp = query.value(2).toString();
        QString layerTemp = query.value(3).toString();
        QString roomTemp = query.value(4).toString();
        QString devNameTemp = query.value(5).toString();

        QString pos = areaTemp + ridgepoleTemp + layerTemp + roomTemp;
        item[i]->setPos(pos);
        item[i]->setLogo(devNameTemp);
        i++;
    }

    count = i;
    bottomLayout= new QGridLayout;
    for(i=0; i<count; i++)
    {
        bottomLayout->addWidget(item[i], i/5, i%5);
    }
    qDebug() << i;
    if(count < 20)
    {
        for(; i<20; i++)
        {
            item[i] = new ResultItem;
            bottomLayout->addWidget(item[i], i/5, i%5);
        }
    }

    if(i != 0)
    {
        if(isEmpty == 0)
        {
            tmpLabel->hide();
            groupBox->setLayout(bottomLayout);
            mainLayout->addWidget(groupBox);
        }
        else
        {
            tmpLabel->hide();
            mainLayout->removeWidget(groupBox);
            groupBox->close();
            groupBox = new QGroupBox(this);
            groupBox->setLayout(bottomLayout);
            mainLayout->addWidget(groupBox);
        }
        isEmpty = 1;
    }
    else
    {
        groupBox->close();
        tmpLabel->show();

    }

    QSqlDatabase::database().commit();
}
